<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="父节点" prop="dep_fname">
        <el-cascader
          v-model="form.dep_fid"
          clearable
          :options="treeData"
          placeholder="请选择父节点"
          :props="defaultProps"
        />
      </el-form-item>
      <el-form-item label="名称" prop="dep_name">
        <el-input v-model="form.dep_name" />
      </el-form-item>
      <el-form-item label="排序" prop="dep_sort">
        <el-input-number
          v-model="form.dep_sort"
          :step="1"
          step-strictly
          @change="numberChange"
        />
      </el-form-item>
      <el-form-item v-if="title == '编辑'" label="部门状态" prop="dep_status">
        <el-switch
          v-model="form.dep_status"
          active-text="显示"
          :active-value="1"
          inactive-text="隐藏"
          :inactive-value="0"
          @change="switchChange(form)"
        />
      </el-form-item>
      <el-form-item label="说明" prop="dep_desc">
        <el-input v-model="form.dep_desc" type="textarea" />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { getList, doAdd, doEdit, doSwitch } from '@/api/organization'

  export default {
    name: 'OrganizationEdit',
    data() {
      return {
        treeData: [],
        defaultProps: {
          children: 'sub_dep',
          label: 'dep_name',
          value: 'id',
          checkStrictly: true,
          emitPath: false,
        },
        form: {
          dep_name: '',
          dep_sort: 0,
        },
        saveData: {
          dep_id: '',
          dep_fid: '',
          dep_name: '',
          dep_desc: '',
          dep_sort: '',
          sort: '',
        },
        switchData: {
          dep_id: '',
          status_name: 'dep_status',
          status_value: '',
        },
        rules: {
          dep_name: [
            { required: true, trigger: 'blur', message: '请输入名称' },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      async fetchData() {
        const data = await getList({})
        this.treeData = data.data.length > 0 ? data.data : []
      },
      numberChange(value) {
        this.form.dep_sort = value
        this.$forceUpdate()
      },
      handleNodeClick(node) {
        this.form.dep_fname = node.dep_name
        this.form.dep_fid = node.id
        this.$forceUpdate()
      },
      async switchChange(form) {
        this.switchData.dep_id = form.id
        this.switchData.status_value = form.dep_status
        await doSwitch(this.switchData)
      },
      showEdit(row) {
        if (!row) {
          this.title = '添加'
        } else {
          this.title = '编辑'
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.title == '添加') {
              this.form.sort = this.form.dep_sort
              const { data } = await doAdd(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '修改成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
              this.$emit('fetch-data')
              this.close()
            } else if (this.title == '编辑') {
              this.form.dep_id = this.form.id
              this.form.sort = this.form.dep_sort
              const { data } = await doEdit(this.form)
              if (data.is_success) {
                this.$baseMessage(
                  '修改成功',
                  'success',
                  'vab-hey-message-success'
                )
              }
              this.$emit('fetch-data')
              this.close()
            }
          }
        })
      },
    },
  }
</script>
